#include "stdafx.h"
#include "../client/DCPlusPlus.h"
#include "Resource.h"

#include "../client/SettingsManager.h"
#include "DetectorPage.h"

#include "WinUtil.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
PropPage::Item DetectorPage::items[] = {
	{ IDC_MAX_TESTSURS, SettingsManager::MAX_TESTSURS, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_MAX_FILELISTS, SettingsManager::MAX_FILELISTS, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_CHECK_DELAY, SettingsManager::CHECK_DELAY, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_FLOOD_COUNTER, SettingsManager::FLOOD_COUNTER, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_FLOOD_TIME, SettingsManager::FLOOD_TIME, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_FLOOD_NOTIFY, SettingsManager::FLOOD_NOTIFY, PropPage::T_BOOL },
	{ IDC_FLOOD_KICK, SettingsManager::FLOOD_KICK, PropPage::T_BOOL },
	{ IDC_PROTECTED_USERS, SettingsManager::PROTECTED_USERS, PropPage::T_STR },
	{ IDC_ACCEPTED_DISCONNECTS, SettingsManager::ACCEPTED_DISCONNECTS, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_ACCEPTED_TIMEOUTS, SettingsManager::ACCEPTED_TIMEOUTS, PropPage::T_INT_SHOWDEFAULT },
	{ IDC_SLEEP_TIME, SettingsManager::SLEEP_TIME, PropPage::T_INT_SHOWDEFAULT },
	{ 0, 0, PropPage::T_END }
};

LRESULT DetectorPage::onInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
	PropPage::read((HWND)*this, items);
	//// Do specialized reading here
	addItem(SettingsManager::DISCONNECT_RAW,				SettingsManager::DISPLAY_DISCONNECT_CHEAT,					"Disconnect raw");
	addItem(SettingsManager::TIMEOUT_RAW,					SettingsManager::DISPLAY_TIMEOUT_CHEAT,						"Timeout raw");
	addItem(SettingsManager::NO_SLOTS_DCPP,					SettingsManager::DISPLAY_NO_SLOTS_DCPP_CHEAT,				"No slots DC++");
	addItem(SettingsManager::FILELIST_TOO_BIG,				SettingsManager::DISPLAY_FILELIST_TOO_BIG_CHEAT,			"Filelist too big");
	addItem(SettingsManager::FILELIST_TOO_SMALL,			SettingsManager::DISPLAY_FILELIST_TOO_SMALL_CHEAT,			"Filelist too small");
	addItem(SettingsManager::FILELIST_UNAVAILABLE,			SettingsManager::DISPLAY_FILELIST_UNAVAILABLE_CHEAT,		"Filelist unavailable");
	addItem(SettingsManager::SMALL_FILE_ERROR,				SettingsManager::DISPLAY_SMALL_FILE_ERROR_CHEAT,			"Small file error");
	addItem(SettingsManager::LISTLEN_MISMATCH,				SettingsManager::DISPLAY_LISTLEN_MISMATCH_CHEAT,			"ListLen error");
	addItem(SettingsManager::LISTLEN_ELEVEN,				SettingsManager::DISPLAY_LISTLEN_ELEVEN_CHEAT,				"ListLen = 11 B");
	addItem(SettingsManager::VERSION_MISMATCH,				SettingsManager::DISPLAY_VERSION_MISMATCH_CHEAT,			"Version mis-match");
	addItem(SettingsManager::BAD_ISP,						SettingsManager::DISPLAY_BAD_ISP_CHEAT,						"Bad ISP");
	addItem(SettingsManager::REVERSE_CONNECT,				SettingsManager::DISPLAY_REVERSE_CONNECT_CHEAT,				"Reverse connect");
	addItem(SettingsManager::RMDC,							SettingsManager::DISPLAY_RMDC_CHEAT,						"rmDC");
	addItem(SettingsManager::TO_MANY_IPS_RAW,				SettingsManager::DISPLAY_TO_MANY_IPS_CHEAT,					"To many connections from same IP");
	addItem(SettingsManager::INFLATED_FILELIST_RAW,			SettingsManager::DISPLAY_INFLATED_FILELIST_CHEAT,			"Inflated Filelist");
	addItem(SettingsManager::DIVISIBLE_BY_1MB_RAW,			SettingsManager::DISPLAY_DIVISIBLE_BY_1MB_CHEAT,			"Share size divisible by 1 MB");
	addItem(SettingsManager::TOO_MANY_OF_SAME_NUMBERS_RAW,	SettingsManager::DISPLAY_TOO_MANY_OF_SAME_NUMBERS_CHEAT,	"To many occurances of same digit in share size");
	addItem(SettingsManager::ACTIVE_USING_PASSIVE_RAW,		SettingsManager::DISPLAY_ACTIVE_USING_PASSIVE,				"Tag states active mode but is using passive commands");

	CRect rc;
	ctrlList.Attach(GetDlgItem(IDC_DETECTOR_ITEMS));
	ctrlList.GetClientRect(rc);
	ctrlList.InsertColumn(0, CTSTRING(NAME), LVCFMT_LEFT, (rc.Width() / 3), 0);
	ctrlList.InsertColumn(1, CTSTRING(RAW_COMMAND), LVCFMT_LEFT, (rc.Width() / 3 - 20), 1);
	ctrlList.InsertColumn(2, CTSTRING(SETTINGS_DISPLAY_CHEATS_IN_MAIN_CHAT), LVCFMT_LEFT, (rc.Width() / 3 + 10), 2);
	
	if(BOOLSETTING(FULL_ROW_SELECT)){
		ctrlList.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT);
	}
	for(DetectorList::Iter i = detectList.begin(); i != detectList.end(); ++i)
		insertItem(
			(*i)->getText(), 
			SettingsManager::getInstance()->get((SettingsManager::IntSetting)(*i)->getRaw()), 
			Util::toBool(SettingsManager::getInstance()->get((SettingsManager::IntSetting)(*i)->getDisplay()))
		);

	SetDlgItemText(IDC_CONNECTIONS, Util::toString(SETTING(MAX_CONNECTION_SETTING)).c_str());
	//End
	return TRUE;
}

void DetectorPage::write()
{
	PropPage::write((HWND)*this, items);
	
	// Do specialized writing here
	TCHAR buf[32];
	GetDlgItemText(IDC_CONNECTIONS, buf, 32);
	int connections = Util::toInt((string)buf);
	if(connections<10)connections = 10;

	settings->set(SettingsManager::MAX_CONNECTION_SETTING, connections);
}

//Sulan start svn73
LRESULT DetectorPage::onChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/) {
	
	DetectorPageDlg dlg;
	if(ctrlList.GetSelectedCount() == 1) {
		int sel = ctrlList.GetNextItem(-1, LVNI_SELECTED);
		dlg.raw = SettingsManager::getInstance()->get((SettingsManager::IntSetting)detectList[sel]->getRaw());
		dlg.text = detectList[sel]->getText();
		dlg.display = Util::toBool(SettingsManager::getInstance()->get((SettingsManager::IntSetting)detectList[sel]->getDisplay()));
		if(dlg.DoModal() == IDOK) {
			settings->set((SettingsManager::IntSetting)detectList[sel]->getRaw(), dlg.raw);
			settings->set((SettingsManager::IntSetting)detectList[sel]->getDisplay(), dlg.display);
			ctrlList.SetItemText(sel, 1, RAWSETTING(dlg.raw).c_str());
			ctrlList.SetItemText(sel, 2, WinUtil::toYesNo(dlg.display).c_str());
		}
	}
	return TRUE;
}
//End